﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Site.Domain.Core.Abstract;

namespace Site.Domain.Core.Concrete
{
    public class Page : IPage
    {
        private List<IBlock> blocks = new List<IBlock>();

        private const int DEFAULT_WIDTH = 800;
        private const int DEFAULT_HEIGHT = 600;

        internal Page()
        {
            Size = new PageSize { Width = DEFAULT_WIDTH, Height = DEFAULT_HEIGHT };
        }

        public string Name { get; set; }

        public string Title { get; set; }

        private ILayout layout;
        public ILayout Layout 
        { 
            get { return layout;}
            set 
            {
                if (layout != null)
                {
                    layout.OnRearranged -= LayoutRearranged;
                }
                layout = value;
                layout.OnRearranged += LayoutRearranged;
            } 
        }

        private void LayoutRearranged(object sender, EventArgs args)
        {
            FireOnPageRearranged();
        }

        public PageSize Size { get; set; }

        public ICollection<IBlock> Blocks
        {
            get { return blocks; }
        }

        public int BlockCount()
        {            
            return Blocks.Count;
        }

        private void FireOnPageRearranged()
        {
            if (OnPageRearranged != null)
            {
                OnPageRearranged(this, null);
            }
        }

        public event EventHandler OnPageRearranged;
    }
}
